package com.sixdog.regulate.utils;

import com.sixdog.regulate.bean.Device;
import com.sixdog.regulate.configuration.GlobalConfiguration;
import com.sixdog.regulate.service.DeviceService;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import java.io.*;
import java.rmi.server.ExportException;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

@Component
public class DeviceUtils implements InitializingBean{

    // 用来存放所有的设备信息
    public static final transient Map<String, Device> devices = new ConcurrentHashMap<>();

    @Autowired
    private DeviceService service;

    @Autowired
    private GlobalConfiguration configuration;

    private static DeviceService deviceService;

    private static String saveLocation;

    /**
     * 通过序列号获取手机设备信息
     */
    public static Device getDeviceInfoBySerialNo(String serialNo){
        checkSerialNo(serialNo);
        Device device = new Device(
                serialNo,
                deviceService.getDeviceModel(serialNo),
                deviceService.getDeviceBrand(serialNo),
                deviceService.getDeviceDpi(serialNo),
                deviceService.getAndroidId(serialNo),
                deviceService.getAndroidVersion(serialNo),
                deviceService.getPhysicalSize(serialNo));
        addDevices(serialNo,device);
        return device;
    }

    /**
     * 通过序列号批量获取手机设备信息
     * @param serialNo 序列号
     * @return Device对象
     */
    public static List<Device> getDeviceInfoBySerialNos(String ...serialNo){
        checkSerialNo(serialNo);
        List<Device> devices = new ArrayList<>();
        Device device = null;
        for(String serial : serialNo){
            if(!StringUtils.isBlank(serial)) {
                device = new Device(
                        serial,
                        deviceService.getDeviceModel(serial),
                        deviceService.getDeviceBrand(serial),
                        deviceService.getDeviceDpi(serial),
                        deviceService.getAndroidId(serial),
                        deviceService.getAndroidVersion(serial),
                        deviceService.getPhysicalSize(serial));
                devices.add(device);
            }
        }
        addDevices(devices);
        return devices;
    }

    /**
     * 将设备存放到集合中
     * @param serialNo 序列号
     * @param device 序列号对应的Device对象
     */
    private static void addDevices(String serialNo, Device device){
        devices.put(serialNo,device);
    }

    private static void addDevices(List<Device> devices){
        //devices.put(serialNo,device);
        // 获取已经保存的所有设备序列号信息
        if(devices != null && devices.size() > 0){
            devices.forEach(item -> {
                DeviceUtils.devices.put(item.getSerialNo(),item);
            });
        }
    }



    private static void checkSerialNo(String ...serialNo){
        if(serialNo != null && serialNo.length > 0){
            for(String no : serialNo){
                if(StringUtils.isBlank(no)) throw new RuntimeException("serialNo不能为空!");
            }
            return;
        }
        throw new RuntimeException("serialNo不能为空!");
    }


    public static void serialDevices() throws IOException{
        File file = new File(saveLocation);
        if(!file.exists()){
            file.mkdirs();
        }
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(saveLocation + "devices"));
        oos.writeObject(DeviceUtils.devices);
        System.out.println("序列化的内容:" + DeviceUtils.devices);
        oos.close();
    }

    public static Map<String, Device> deserializationDevices() throws IOException, ClassNotFoundException {
        File file = new File(saveLocation + "devices");
        if(!file.exists()){
            throw new RuntimeException("读取保存的设备信息失败,原因:指定目录不存在!");
        }
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream(saveLocation + "devices"));
        Map<String,Device> readObject = (Map<String, Device>) ois.readObject();
        ois.close();
        return readObject;
    }


    @Override
    public void afterPropertiesSet() throws Exception {
        deviceService = this.service;
        saveLocation = configuration.getSaveLocation();
    }
}
